package com.example.view.module.animations;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.common.base.AbsActivity;
import com.example.view.R;
import com.example.view.databinding.ActAnimationMainBinding;

/**
 * 动画
 * 一、帧动画【DrawableAnimation】
 * clearAnimation 取消动画
 * 属性动画的属性
 */
@Route(path = "/animation/example")
public class AnimationActivity extends AbsActivity<ActAnimationMainBinding> {

    private AnimationDrawable animationDrawable;
    private ObjectAnimator translate;

    @Override
    protected int getLayoutId() {
        return R.layout.act_animation_main;
    }

    @Override
    protected void main() {

    }

    @Override
    protected void initListener() {

        //动画监听
       translate.addListener(new AnimatorListenerAdapter() {
           @Override
           public void onAnimationStart(Animator animation) {
               //动画开始时执行
           }
           @Override
           public void onAnimationEnd(Animator animation) {
               //动画结束时执行
           }
           @Override
           public void onAnimationCancel(Animator animation) {
               //动画取消时执行
           }
           @Override
           public void onAnimationRepeat(Animator animation) {
               //动画重复时执行
           }
       });

       translate.addListener(new Animator.AnimatorListener() {
           @Override
           public void onAnimationStart(Animator animation) {

           }

           @Override
           public void onAnimationEnd(Animator animation) {

           }

           @Override
           public void onAnimationCancel(Animator animation) {

           }

           @Override
           public void onAnimationRepeat(Animator animation) {

           }
       });

        //帧动画
        mBinding.ivDrawableAnimation.setOnClickListener(v -> {
            animationDrawable = (AnimationDrawable) mBinding.ivDrawableAnimation.getDrawable();
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
            } else {
                animationDrawable.start();
            }
        });
        //动态加载帧动画
        mBinding.btnDrawableAnimation2.setOnClickListener(v -> {
            AnimationDrawable animationDrawable = new AnimationDrawable();
            animationDrawable.setOneShot(false);
            for (int i = 1; i <= 4; i++) {
                int id = getResources().getIdentifier("girl" + i, "mipmap", getPackageName());
                Drawable drawable = getResources().getDrawable(id);
                animationDrawable.addFrame(drawable, 100);
            }
            mBinding.ivDrawableAnimation.setImageDrawable(animationDrawable);
            AnimationDrawable drawable = (AnimationDrawable) mBinding.ivDrawableAnimation.getDrawable();
            if (drawable.isRunning()) {
                drawable.stop();
            } else {
                drawable.start();
            }
        });

        //补间 Alpha 动画
        mBinding.btnAlpha.setOnClickListener(v -> {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            alphaAnimation.setDuration(1000);        //持续时间
            alphaAnimation.setFillAfter(true);      //动画结束保留结束状态
            alphaAnimation.setInterpolator(new AccelerateInterpolator());       //添加插值器
            mBinding.btnAlpha.setAnimation(alphaAnimation);
            mBinding.btnAlpha.startAnimation(alphaAnimation);
        });

        //补间 translate 动画
        mBinding.btnScale.setOnClickListener(v -> {
            ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(2000);
            scaleAnimation.setFillAfter(false);
            mBinding.btnScale.setAnimation(scaleAnimation);
            mBinding.btnScale.startAnimation(scaleAnimation);
        });

        //tween translate animation
        mBinding.btnTranslate.setOnClickListener(v -> {
            TranslateAnimation translateAnimation = new TranslateAnimation(100, 500, 100, 500);
            translateAnimation.setDuration(2000);
            translateAnimation.setFillAfter(false);
            mBinding.btnTranslate.setAnimation(translateAnimation);
            mBinding.btnTranslate.startAnimation(translateAnimation);
        });

        //tween rotate animation
        mBinding.btnRotate.setOnClickListener(v -> {
            RotateAnimation rotateAnimation = new RotateAnimation(0, 190, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
            rotateAnimation.setDuration(2000);
            rotateAnimation.setFillAfter(false);
            mBinding.btnRotate.setAnimation(rotateAnimation);
            mBinding.btnRotate.startAnimation(rotateAnimation);
        });

        //property set xml
        mBinding.btnSetXml.setOnClickListener(v -> {
            //创建组合动画对象，加载xml
            AnimatorSet animationSet = (AnimatorSet) AnimatorInflater.loadAnimator(mContext, R.animator.anim_property_set);
            animationSet.setTarget(mBinding.btnSetXml);
            animationSet.start();
        });

        //property objectAnimator
        mBinding.btnObjectAnimator.setOnClickListener(v -> {
            //平移
            translate = ObjectAnimator.ofFloat(mBinding.btnObjectAnimator, "translationX", 0, 300, 0);
            ObjectAnimator rotate = ObjectAnimator.ofFloat(mBinding.btnObjectAnimator, "rotation", 0f, 360f);               //旋转
            ObjectAnimator alpha = ObjectAnimator.ofFloat(mBinding.btnObjectAnimator, "alpha", 1f, 0f, 1f);                 //透明度
            AnimatorSet animatorSet = new AnimatorSet();        //创建组合动画的对象
            animatorSet.play(translate).with(rotate).before(alpha);
            animatorSet.setDuration(3000);
            animatorSet.start();
//        AnimatorSet.play(Animator anim)   ：播放当前动画
//        AnimatorSet.after(long delay)   ：将现有动画延迟x毫秒后执行
//        AnimatorSet.with(Animator anim)   ：将现有动画和传入的动画同时执行
//        AnimatorSet.after(Animator anim)   ：将现有动画插入到传入的动画之后执行
//        AnimatorSet.before(Animator anim) ：  将现有动画插入到传入的动画之前执行

        });

        //property valueAnimator    初始值 以整型数值的形式 过渡到结束值
        mBinding.btnValueAnimator.setOnClickListener(v -> {
            ValueAnimator valueAnimator = setValueAnimator(mBinding.btnValueAnimator, 0, 2, 2000, 1000, 2);
            valueAnimator.start();
        });

        //property valueAnimator xml
        mBinding.btnValueAnimatorXml.setOnClickListener(v -> {
            Animator mAnim = AnimatorInflater.loadAnimator(this, R.animator.anim_property_value);
            mAnim.setTarget(mBinding.btnValueAnimatorXml);
            mAnim.start();
        });
    }

    private ValueAnimator setValueAnimator(View view, int start, int end, int time, int delay, int count) {
        //设置动画属性的初始值和结束值
        //1.创建实例2.平滑过度
        //如果传入了3个Int参数 a,b,c ,则是先从a平滑过渡到b,再从b平滑过渡到C，以此类推
        //内置了整型估值器,直接采用默认的.不需要设置，即默认设置了如何从初始值 过渡到 结束值
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(start, end);
        valueAnimator.setTarget(view);
        valueAnimator.setDuration(time);        //动画执行时长
        valueAnimator.setStartDelay(delay);      //动画延时时长
        valueAnimator.setRepeatCount(count);    //动画重复播放次数=count+1；infinite无限播放
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);     //重复播放动画模式，  ValueAnimator.RESTART(默认):正序重放；ValueAnimator.REVERSE:倒序回放
        return valueAnimator;
    }

}
